﻿namespace EasyCMS.Common
{
    using System;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;

    public class Runtime
    {
        private static string m_ConfigPath;
        private static Components.Application m_Application = null;

        #region ObjectContainer
        public static IUnityContainer ObjectContainer
        {
            get
            {
                if (m_ObjectContainer == null)
                {
                    m_ObjectContainer = new UnityContainer();
                    UnityConfigurationSection section = (UnityConfigurationSection)EasyCMS.Common.Runtime.Application.Configuration.GetSection("unity");
                    section.Containers["EasyCMS.ObjectContainer"].Configure(m_ObjectContainer);
                }
                return m_ObjectContainer;
            }
        }
        private static IUnityContainer m_ObjectContainer = null;
        #endregion

        #region Application
        /// <summary>
        /// 当前运行的程序域
        /// </summary>
        public static Components.Application Application
        {
            get
            {
                return m_Application;
            }
        }
        #endregion

        #region ConfigPath
        /// <summary>
        /// 配置文件所在的路径
        /// </summary>
        public static string ConfigPath
        {
            get
            {
                return m_ConfigPath;
            }
        }
        #endregion

        #region 构造
        /// <summary>
        /// 
        /// </summary>
        static Runtime()
        {
            //获取配置文件根路径Fv3K7N4s
            if (System.Configuration.ConfigurationManager.AppSettings["EasyCMSConfigPath"] != null)
            {
                m_ConfigPath = System.Configuration.ConfigurationManager.AppSettings["EasyCMSConfigPath"];
                m_ConfigPath = m_ConfigPath.Replace("~/", AppDomain.CurrentDomain.SetupInformation.ApplicationBase.TrimEnd('\\') + "\\");
            }
            else
            {
                m_ConfigPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase.TrimEnd('\\') + "\\MyConfig";
            }
            m_ConfigPath = m_ConfigPath.TrimEnd('/');
            m_ConfigPath = m_ConfigPath.TrimEnd('\\');

            //初始化Application
            m_Application = Components.Application.Instance();

            //监试配置文件，变化后重启应用程序
            ApplicationSettingWatcher = new System.IO.FileSystemWatcher(ConfigPath, "EasyCMS.config");
            ApplicationSettingWatcher.Changed += new System.IO.FileSystemEventHandler(EasyCMS_Run);
            ApplicationSettingWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite;
            ApplicationSettingWatcher.EnableRaisingEvents = true;
        }
        private static System.IO.FileSystemWatcher ApplicationSettingWatcher;
        private static void EasyCMS_Run(object sender, System.IO.FileSystemEventArgs e)
        {
            System.Web.HttpRuntime.UnloadAppDomain();
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="message"></param>
        public static void WriteLog(Pismire.Loger.LogType type, string message)
        {
            Pismire.Runtime.WriteLog("EasyCMS", type, message, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        public static void WriteLog(Pismire.Loger.LogType type, string message, Exception ex)
        {
            Pismire.Runtime.WriteLog("EasyCMS", type, message, ex);
        }
    }
}
